package model.forview;

import java.sql.Date;
import java.util.ArrayList;

import sun.text.resources.FormatData;

import model.wrap.node.Node;
import models.ConstantName;
import models.ConstantValue;

public class SearchModelForView extends ContentForMainTemplate
{
    private ArrayList<Node> nodes;
    private String query;

    public SearchModelForView(String query)
    {
	this.query = query;
	this.nodes = Node.search(query);
	setDefaultValues();
    }

    public int getNodesSize()
    {
	return nodes.size();
    }

    public String getNodeTitle(int index)
    {
	return nodes.get(index).getTitle();
    }
    
    public String getStructureTitle(int index)
    {
	return nodes.get(index).getStructure().getTitle();
    }

    public Long getStructureId(int index)
    {
	return nodes.get(index).getStructure().getId();
    }
    
    public String getNodesCreaterName(int index)
    {
	String fullName = "";
	try
	{
	    fullName = nodes.get(index).getCreater().getFullName();
	}
	catch (Exception e)
	{
	    fullName = "";
	}

	return fullName;
    }
    
    public String getNodesChangerName(int index)
    {
	String fullName = "";
	try
	{
	    fullName = nodes.get(index).getChanger().getFullName();
	}
	catch (Exception e)
	{
	    fullName = "";
	}

	return fullName;
    }

    public String getNodesCreateDate(int index)
    {
	String fullName = "";
	try
	{
	    fullName = nodes.get(index).getCreateFormatDate();
	}
	catch (Exception e)
	{
	    fullName = "";
	}

	return fullName;
    }

    public String getNodesChangeDate(int index)
    {
	String fullName = "";
	try
	{
	    fullName = nodes.get(index).getChangeFormatDate();
	}
	catch (Exception e)
	{
	    fullName = "";
	}

	return fullName;
    }

    
    public Long getNodeId(int index)
    {
	return (nodes.get(index).getId());
    }

    /**
     *Возвращает индек для шаблона
     * @param index
     * @return
     */
    public String getIndex(int index)
    {
        return String.valueOf(index + 1);
    }

    public void setDefaultValues()
    {
	String title = ConstantValue
		.getConstantValue(ConstantName.STRUCTURES_PAGE_TITLE);
	title = title != null ? title : "title";
	String userName = "";
	String header = ConstantValue
		.getConstantValue(ConstantName.STRUCTURES_PAGE_HEADER);
	header = header != null ? header : "header";
	String footer = ConstantValue
		.getConstantValue(ConstantName.STRUCTURES_PAGE_FOOTER);
	footer = footer != null ? footer : "footer";
	mainModelForView = new MainModelForView(title, userName, header, footer);
    }
    
    public String getQuery()
    {
	return this.query;
    }

}
